package thread_unsafe;

/**
 * @Description: 懒汉式, 线程不安全, 多个线程同时调用 getInstance() 方法, 可能会创建多个对象
 * @Author: WangBlue
 * @Create: 2024-09-23 14:46
 * @Version: 1.0
 **/

public class Unfase {
    // 私有构造函数，防止实例化
    private Unfase() {  };

    // 在成员位置创建该类的对象
    private static Unfase instance;

    public static Unfase getInstance() {
        if (instance == null) {
            instance = new Unfase();
        }
        return instance;
    }
}

class Test{
    public static void main(String[] args) {
        Unfase instance = Unfase.getInstance();
        Unfase instance2 = Unfase.getInstance();
        System.out.println("懒汉式的单例模式测试，线程不安全");
        if (instance == instance2) {
            System.out.println("两个对象是同一个");
        }
    }
}
